package com.ruoyi.web.controller.biz;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.BsCommentConstants;
import com.ruoyi.common.constant.BsSysConfigConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.system.domain.ro.BsCommentRO;
import com.ruoyi.system.domain.vo.BsCommentVO;
import com.ruoyi.system.service.IBsCommentService;
import com.ruoyi.system.service.impl.BsSysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 评论记录
 *
 * @author ruoyi
 * @date 2023-11-25
 */
@Validated
@RequiredArgsConstructor
@Api(tags = "游客端-攻略游记")
@RestController
@RequestMapping("/system/comment")
public class BsCommentController extends BaseController {

    private final IBsCommentService iBsCommentService;
    private final BsSysConfigService bsSysConfigService;

    @ApiOperation(value = "攻略游记评论-评论列表", tags = {"游客端-攻略游记"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "攻略游记id"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsCommentVO>> list(@RequestParam() String aimId) {
        BsCommentRO ro = BsCommentRO.builder().aimId(aimId).type(BsCommentConstants.TYPE_STRATEGY).build();
        startPage();
        List<BsCommentVO> list = iBsCommentService.queryList(ro);
        return Result.success(getDataTable(list));
    }

    @ApiOperation(value = "攻略游记评论-评论详情", tags = {"游客端-攻略游记"})
    @GetMapping("/{id}")
    public Result<BsCommentVO> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return Result.success(iBsCommentService.queryById(id));
    }

    @ApiOperation(value = "攻略游记评论-评论新增", tags = {"游客端-攻略游记"})
    @Log(title = "评论记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public Result<?> add(@Validated(ValidInsertGroup.class) @RequestBody BsCommentRO ro) {
        ro.setType(BsCommentConstants.TYPE_STRATEGY);
        if (!bsSysConfigService.allowBySysSecretWord(ro.getContent())) {
            return Result.error(BsSysConfigConstants.NOT_ALLOW_BY_SYS_SECRET_WORD);
        }
        return Result.toResult(iBsCommentService.insertByBo(ro));
    }
}
